package com.xfsong.demo.controller;

import com.xfsong.demo.common.R;
import com.xfsong.demo.entity.Goods;
import com.xfsong.demo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    //列表查询
    @GetMapping
    public R index(@RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "5") Integer size,
                   @RequestParam(value = "field", defaultValue = "price") String field, @RequestParam(value = "sort", defaultValue = "asc") String sort,
                   @RequestParam(value = "name", required = false) String name) {
        Sort sortOrder;
        if ("desc".equalsIgnoreCase(sort)) {
            sortOrder = Sort.by(Sort.Direction.DESC, field);
        } else {
            sortOrder = Sort.by(Sort.Direction.ASC, field);
        }
        Pageable pageable = PageRequest.of(page - 1, size, sortOrder);
        Page<Goods> goodsPage;
        if (name != null && !name.isEmpty()) {
            goodsPage = goodsService.getGoodsByNameAndPage(name, pageable);
        } else {
            goodsPage = goodsService.getGoodsByPageAndSort(pageable);
        }
        return R.ok(goodsPage);
    }

    //新增
    @PostMapping
    public R addGoods(@RequestBody Goods goods) {
        //商品名称不能为空
        if (goods.getName() == null || goods.getName().trim().length() == 0) {
            return R.fail("商品名称不能为空");
        }
        //商品价格不能为空
        if (goods.getPrice() == 0) {
            return R.fail("商品价格不能为空");
        }

        //商品名称不能重复
        if (goodsService.isGoodsNameExist(goods.getName())) {
            return R.fail("商品名称不能重复");
        }
        return R.ok(goodsService.saveGoods(goods));
    }

    //修改
    @PutMapping("/{id}")
    public R edit(@RequestBody Goods goods, @PathVariable("id") Long id) {
        Optional<Goods> existingGoods = Optional.ofNullable(goodsService.getGoodsById(id));
        if (existingGoods.isPresent()) {
            Goods updatedGoods = existingGoods.get();
            updatedGoods.setName(goods.getName());
            updatedGoods.setPrice(goods.getPrice());
            Goods savedGoods = goodsService.updateGoods(updatedGoods);
            return R.ok(savedGoods);

        } else {
            return R.fail("商品不存在");
        }
    }

    //删除
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id) {
        //删除商品
        if (id == null) {
            return R.fail("商品id不能为空");
        }

        //判断商品是否存在
        Goods goodsInfo = goodsService.getGoodsById(id);
        if (goodsInfo == null) {
            return R.fail("商品不存在");
        }

        goodsService.deleteGoodsById(id);
        return R.ok("删除成功");

    }

    @GetMapping("/deepseak")
    public void deepseak() {
        System.out.println("deepseak");
    }
}
